home *** CD-ROM | disk | FTP | other *** search
/ Network CD 2 / Network CD - Volume 2.iso / programs / internet / mail / smailsr2.lha / SMail / src / RCS / batch_smtp.c,v < prev    next >
Encoding:
Text File  |  1993-11-30  |  11.4 KB  |  639 lines

  1. head    2.2;
  2. access;
  3. symbols
  4.     C_2:2.2
  5.     C_1:1.6;
  6. locks; strict;
  7. comment    @ * @;
  8.  
  9.  
  10. 2.2
  11. date    93.11.29.23.03.30;    author Aussem;    state Exp;
  12. branches;
  13. next    2.1;
  14.  
  15. 2.1
  16. date    93.11.23.19.58.39;    author Aussem;    state Exp;
  17. branches;
  18. next    1.6;
  19.  
  20. 1.6
  21. date    93.11.15.22.34.16;    author Aussem;    state Exp;
  22. branches;
  23. next    1.5;
  24.  
  25. 1.5
  26. date    93.10.31.14.23.31;    author Aussem;    state Exp;
  27. branches;
  28. next    1.4;
  29.  
  30. 1.4
  31. date    93.10.28.23.52.17;    author Aussem;    state Exp;
  32. branches;
  33. next    1.3;
  34.  
  35. 1.3
  36. date    93.10.28.23.30.31;    author Aussem;    state Exp;
  37. branches;
  38. next    1.2;
  39.  
  40. 1.2
  41. date    93.10.28.23.15.11;    author Aussem;    state Exp;
  42. branches;
  43. next    1.1;
  44.  
  45. 1.1
  46. date    93.10.17.21.44.32;    author Aussem;    state Exp;
  47. branches;
  48. next    ;
  49.  
  50.  
  51. desc
  52. @append BSMTP mails to uuspool:batch/hostname
  53. @
  54.  
  55.  
  56. 2.2
  57. log
  58. @version strings updated
  59. @
  60. text
  61. @/*
  62.  * Batches a mail to uuspool:batch/<host>
  63.  * in BSMTP format
  64.  *
  65.  * This program is free software; you can redistribute it and/or
  66.  * modify it under the terms of the GNU General Public License as
  67.  * published by the Free Software Foundation; either version 2 of
  68.  * the License, or (at your option) any later version.
  69.  *
  70.  * This program is distributed in the hope that it will be useful,
  71.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  72.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  73.  * General Public License for more details.
  74.  *
  75.  * You should have received a copy of the GNU General Public License
  76.  * along with this program; if not, write to the Free Software
  77.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  78.  *
  79.  * $Log: batch_smtp.c,v $
  80.  * Revision 2.1  1993/11/23  19:58:39  Aussem
  81.  * Version 2.0 check in
  82.  *
  83.  * Revision 1.6  1993/11/15  22:34:16  Aussem
  84.  * NOFROM define for removing the from line
  85.  * CRLF for adding \n\r to the mail (needed for SMTP over TCPIP)
  86.  *
  87.  * Revision 1.5  1993/10/31  14:23:31  Aussem
  88.  * \n -> \r\n conversion removed
  89.  * only need in SMTP with TCPIP
  90.  *
  91.  * Revision 1.4  1993/10/28  23:52:17  Aussem
  92.  * fmode() deleted. There are problems with the length of the mail
  93.  *
  94.  * Revision 1.2  1993/10/28  23:15:11  Aussem
  95.  * \n is now converted to \r\n
  96.  * file reading now lines instead the full file
  97.  *
  98.  * Revision 1.1  1993/10/17  21:44:32  Aussem
  99.  * Initial revision
  100.  *
  101.  *
  102.  */
  103.  
  104. static char     *rcsid="$Id: batch_smtp.c,v 2.1 1993/11/23 19:58:39 Aussem Exp Aussem $";
  105.  
  106. #include <stdio.h>
  107. #include <string.h>
  108. #include <stdlib.h>
  109. #include <dos.h>
  110. #include <proto/dos.h>
  111. #include <pragmas/dos_pragmas.h>
  112. #include <proto/exec.h>
  113. #include <pragmas/exec_pragmas.h>
  114.  
  115. extern struct DosLibrary *DOSBase;
  116.  
  117. /* global Vars for this modul */
  118. static const char *uuspool = "UUSPOOL:batch";
  119. static const char *version = "$VER: batch_smtp V2.0 © 1993 by Ralph-Thomas Aussem ("__DATE__","__TIME__")";
  120. static const int maxretry = 30;
  121. static const int retrysecs = 20;
  122. static char *argv0 = "batch_smtp";
  123.  
  124. /* prototypes */
  125. static long filelength (char *);
  126. static void sleep (unsigned long time);
  127.  
  128.  
  129. /*
  130.  * sleep():
  131.  * wait <time> seconds
  132.  */
  133. static void
  134. sleep (unsigned long time)
  135. {
  136.   Delay (50L * time);
  137. }
  138.  
  139. /*
  140.  * filelength():
  141.  * returns the length of file <fname>
  142.  * it returns -1 if the file does not exists or locked 
  143.  * by someone else
  144.  */
  145. static
  146. long
  147. filelength (char *fname)
  148. {
  149.   struct FileInfoBlock __aligned fileinfo;
  150.   struct FileLock *lock;
  151.   long groesse=-2;
  152.  
  153.   if (!(lock = (struct FileLock *) Lock (fname, ACCESS_READ)))
  154.     return (-1);
  155.   Examine ((BPTR) lock, &fileinfo);
  156.   if(fileinfo.fib_DirEntryType<0)
  157.       groesse = (long) fileinfo.fib_Size;
  158.   UnLock ((BPTR) lock);
  159.   return (groesse);
  160. }
  161.  
  162. long getpid(void)
  163. {
  164. struct Process *proc;
  165.  
  166. Forbid();
  167. proc=(struct Process *)FindTask(NULL);
  168. Permit();
  169. if(proc)return(proc->pr_TaskNum);
  170. return(0);
  171. }
  172.  
  173. int lock_with_file(char *lockfilename)
  174. {
  175.     FILE *file;
  176.     char buf[BUFSIZ];
  177.  
  178.     while( ( file = fopen( lockfilename, "r" ) ) != NULL )
  179.     {
  180.         fgets( buf, sizeof(buf), file );
  181.         fclose( file );
  182. #ifdef DEBUG
  183.         printf( "Mail locked by proc '%s'. Waiting 10 secs\n", buf);
  184. #endif
  185.         sleep( 10 );
  186.     }
  187.  
  188. #ifdef DEBUG
  189.     printf("locking Mail.\n");
  190. #endif
  191.     if( ( file = fopen( lockfilename, "w" ) ) == NULL ) return -1;
  192.     fprintf( file, "%d\n", getpid() );
  193.     fclose( file );
  194.     return 0;
  195. }
  196.  
  197. int unlock_with_file( char *lockfilename )
  198. {
  199. #ifdef DEBUG
  200.     printf("unlock Mail.\n");
  201. #endif
  202.     return remove( lockfilename );
  203. }
  204.  
  205.  
  206. /*
  207.  * MAIN
  208.  */
  209. int
  210. main (int argc, char *argv[])
  211. {
  212.   FILE *in,*out;
  213.   char file[128],lockfile[128], puf[128],*host,*batch,*myhost,*p;
  214.   char text[1024];
  215.   long len;
  216.   int rc, retry, flag=0;
  217.  
  218.   argv0 = argv[0];
  219.  
  220.   if (argc != 4)
  221.     {
  222.       printf ("usage %s file host myhostname\n", argv0);
  223.       exit (10);
  224.     }
  225.  
  226.   batch=argv[1];
  227.   host=argv[2];
  228.   myhost=argv[3];
  229.  
  230.   retry=0;
  231.   printf("sending mail to '%s'...",host);
  232.  
  233.   strcpy (lockfile, "T:");
  234.   strcat (lockfile, host);
  235.   strcat (lockfile,".lck");
  236.   lock_with_file(lockfile);
  237.  
  238.   if(filelength((char *)uuspool)!=-2)
  239.       {
  240.    int rc;
  241.  
  242.    rc=mkdir(uuspool);
  243.    if(rc)
  244.        printf("\n%s: error make '%s'\n",argv0,uuspool);
  245.    }
  246.  
  247.   strcpy (file, uuspool);
  248.   AddPart(file,host,sizeof(file));
  249.  
  250.   len=filelength(file);
  251.  
  252.   if(len>0)
  253.       {
  254.       strcpy (puf, "copy ");
  255.       strcat (puf, file);
  256.       strcat (puf, " to ");
  257.       strcat (puf, file);
  258.       strcat (puf, ".o");
  259.       rc = system (puf);
  260.       if (rc)
  261.         {
  262.           printf ("\n%s: Unable to create backup file (%s)\n", argv0, puf);
  263.             unlock_with_file(lockfile);
  264.           exit (10);
  265.         }
  266.    }
  267.   strcat (file, ".o");
  268.  
  269.   do
  270.     {
  271.       retry++;
  272.       out = fopen (file, "r+");
  273.       if (out)
  274.           {
  275.              fseek(out,-strlen("QUIT\n"),SEEK_END);
  276.             break;
  277.             }
  278.       out = fopen (file, "w");
  279.       if (out)
  280.           {
  281.          fprintf(out,"HELO %s\n",myhost);
  282.             break;
  283.             }
  284.       printf ("\n%s: unable to open %s, waiting %d secs\n", argv0, file, retrysecs);
  285.       sleep (retrysecs);
  286.     }
  287.   while (retry < maxretry);
  288.  
  289.   if (!out)
  290.     {
  291.       printf ("\n%s: Unable to open the backup batch-folder '%s' \n", argv0,file);
  292.       remove (file);
  293.         unlock_with_file(lockfile);
  294.       exit (10);
  295.     }
  296.  
  297.  in = fopen (batch, "r");
  298.  if (!in)
  299.     {
  300.       printf ("\n%s: Unable to open the batch file '%s' \n", argv0,batch);
  301.       remove (file);
  302.         unlock_with_file(lockfile);
  303.       exit (10);
  304.     }
  305.  
  306.  while(fgets(text, sizeof(text), in) != NULL)
  307.      {
  308. #ifdef CRLF
  309. /*
  310.  * the conversion from \n to \r\n is
  311.  * only needed for SMTP over TCP/IP connections
  312.  */
  313.          p=strrchr(text,'\n');
  314.          if(p)
  315.              {
  316.             p[0]='\r';
  317.             p[1]='\n';
  318.             p[2]='\0';
  319.              }
  320. #endif /* CRLF */
  321. #ifdef NOFROM
  322. /*
  323.  * removes the From line
  324.  *
  325.  */
  326.          /* from line */
  327.          if(!flag && (strnicmp(text,"From ",5)==0))
  328.                  {flag=1;continue;}
  329.          /* continue line */
  330.          if(flag==1)
  331.          {
  332.                 if(text[0]=='\t')
  333.                  {flag=1;continue;}
  334.              else
  335.                  {flag=2;}
  336.          }
  337. #endif /* NOFROM */
  338.             (void) fputs(text, out);
  339.     }
  340.  
  341.   fclose (in);
  342.   fclose (out);
  343.  
  344.   strcpy (puf, "copy ");
  345.   strcat (puf, file);
  346.   strcat (puf, " to ");
  347.   strcat (puf, file);
  348.   len = strlen (puf);
  349.   if (len >= 2)
  350.     puf[len - 2] = '\0';
  351.   rc = system (puf);
  352.   if (rc)
  353.     {
  354.       printf ("\n%s: Unable to rename backup file (%s)\n", argv0, puf);
  355.       remove (file);
  356.         unlock_with_file(lockfile);
  357.       exit (10);
  358.     }
  359.  
  360.   remove (file);
  361.   unlock_with_file(lockfile);
  362.   exit (0);
  363. }
  364. @
  365.  
  366.  
  367. 2.1
  368. log
  369. @Version 2.0 check in
  370. @
  371. text
  372. @d20 3
  373. d44 1
  374. a44 1
  375. static char     *rcsid="$Id: batch_smtp.c,v 1.6 1993/11/15 22:34:16 Aussem Exp Aussem $";
  376. d59 1
  377. a59 1
  378. static const char *version = "$VER: batch_smtp V1.0 © 1993 by Ralph-Thomas Aussem ("__DATE__","__TIME__")";
  379. a66 1
  380. char * findconfig(char *);
  381. @
  382.  
  383.  
  384. 1.6
  385. log
  386. @NOFROM define for removing the from line
  387. CRLF for adding \n\r to the mail (needed for SMTP over TCPIP)
  388. @
  389. text
  390. @d20 4
  391. d41 1
  392. a41 1
  393. static char     *rcsid="$Id: batch_smtp.c,v 1.5 1993/10/31 14:23:31 Aussem Exp Aussem $";
  394. @
  395.  
  396.  
  397. 1.5
  398. log
  399. @\n -> \r\n conversion removed
  400. only need in SMTP with TCPIP
  401. @
  402. text
  403. @d20 4
  404. d37 1
  405. a37 1
  406. static char     *rcsid="$Id: batch_smtp.c,v 1.4 1993/10/28 23:52:17 Aussem Exp Aussem $";
  407. d150 1
  408. a150 1
  409.   int rc, retry;
  410. d242 5
  411. a247 2
  412. /* the conversion from \n to \r\n is only needed for SMTP on TCP/IP 
  413.    connections
  414. d254 18
  415. a271 1
  416. */
  417. @
  418.  
  419.  
  420. 1.4
  421. log
  422. @fmode() deleted. There are problems with the length of the mail
  423. @
  424. text
  425. @d20 3
  426. d33 1
  427. a33 1
  428. static char     *rcsid="$Id: batch_smtp.c,v 1.2 1993/10/28 23:15:11 Aussem Exp $";
  429. d205 1
  430. a205 1
  431.              fseek(out,-strlen("QUIT\r\n"),SEEK_END);
  432. d211 1
  433. a211 1
  434.          fprintf(out,"HELO %s\r\n",myhost);
  435. d239 2
  436. d247 1
  437. @
  438.  
  439.  
  440. 1.3
  441. log
  442. @converting \n to \r\n by using fmode()
  443. @
  444. text
  445. @d30 1
  446. a30 1
  447. static char     *rcsid="$Id: batch_smtp.c,v 1.2 1993/10/28 23:15:11 Aussem Exp Aussem $";
  448. d140 1
  449. a140 1
  450.   char file[128],lockfile[128], puf[128],*host,*batch,*myhost;
  451. d232 1
  452. a232 1
  453.  fmode(out,0);
  454. d234 10
  455. a243 1
  456.             fputs(text, out);
  457. @
  458.  
  459.  
  460. 1.2
  461. log
  462. @\n is now converted to \r\n
  463. file reading now lines instead the full file
  464. @
  465. text
  466. @d20 4
  467. d30 1
  468. a30 1
  469. static char     *rcsid="$Id: batch_smtp.c,v 1.1 1993/10/17 21:44:32 Aussem Exp Aussem $";
  470. d140 1
  471. a140 1
  472.   char file[128],lockfile[128], puf[128],*host,*batch,*myhost,*p;
  473. d232 1
  474. a232 1
  475.  
  476. d234 1
  477. a234 10
  478.      {
  479.          p=strrchr(text,'\n');
  480.          if(p)
  481.              {
  482.             p[0]='\r';
  483.             p[1]='\n';
  484.             p[2]='\0';
  485.              }
  486.             (void) fputs(text, out);
  487.     }
  488. @
  489.  
  490.  
  491. 1.1
  492. log
  493. @Initial revision
  494. @
  495. text
  496. @d19 3
  497. a21 1
  498.  * $Log:$
  499. d23 1
  500. d26 1
  501. a26 1
  502. static char     *rcsid="$Id$";
  503. a46 1
  504. static char *readfile (char *);
  505. a127 56
  506. /*
  507.  * readfile():
  508.  * read file <filename> and returns the buffer
  509.  *
  510.  */
  511. static
  512. char *
  513. readfile (char *filename)
  514. {
  515.   char *puf;
  516.   size_t len, read;
  517.   FILE *fp;
  518.  
  519. #ifdef DEBUG
  520.     printf("Reading %s.\n",filename);
  521. #endif
  522.  
  523.   if (!filename)
  524.     return (NULL);
  525.  
  526.   len = filelength (filename);
  527.   if (len < 1)
  528.     {
  529.       printf ("\n%s: unable to open file %s\n", argv0, filename);
  530.       return (NULL);
  531.     }
  532.  
  533.   puf = malloc ((len + 1) * sizeof (char));
  534.   if (!puf)
  535.     {
  536.       printf ("\n%s: unable to allocate %d bytes\n", argv0, (len + 1) * sizeof (char));
  537.       free (puf);
  538.       return (NULL);
  539.     }
  540.  
  541.   fp = fopen (filename, "r");
  542.   if (!fp)
  543.     {
  544.       printf ("\n%s: unable to open file %s\n", argv0, filename);
  545.       free (puf);
  546.       return (NULL);
  547.     }
  548.  
  549.   read = fread (puf, len, 1, fp) * len;
  550.   if (read != len)
  551.     {
  552.       printf ("\n%s: unable to read from %s %d (%d) bytes\n", argv0, filename, len, read);
  553.       free (puf);
  554.       fclose (fp);
  555.       return (NULL);
  556.     }
  557.   puf[len]='\0';
  558.   fclose (fp);
  559.   return (puf);
  560. }
  561.  
  562. d135 3
  563. a137 2
  564.   FILE *fp;
  565.   char file[128],lockfile[128], puf[128], *text,*host,*batch,*myhost;
  566. a191 9
  567.   text = readfile (batch);
  568.   if (!text)
  569.     {
  570.       /* error messages are generated by readfile() */
  571.       remove (file);
  572.         unlock_with_file(lockfile);
  573.       exit (10);
  574.     }
  575.  
  576. d195 2
  577. a196 2
  578.       fp = fopen (file, "r+");
  579.       if (fp)
  580. d198 1
  581. a198 1
  582.              fseek(fp,-strlen("QUIT\n"),SEEK_END);
  583. d201 2
  584. a202 2
  585.       fp = fopen (file, "w");
  586.       if (fp)
  587. d204 1
  588. a204 1
  589.          fprintf(fp,"HELO %s\n",myhost);
  590. d212 1
  591. a212 1
  592.   if (!fp)
  593. a215 2
  594.       if (text)
  595.             free (text);
  596. d220 2
  597. a221 29
  598.   len = strlen (text);
  599.  
  600. /*
  601.   {
  602.     register char *c,*last_c,*last_last_c;
  603.  
  604.    last_last_c=text;
  605.    last_c=text+1;
  606.    if(last_c!='\0' && last_last_c!='\0')
  607.        {
  608.        fputc(*last_last_c,fp);
  609.        fputc(*last_c,fp);
  610.        c=text+2;
  611.         while(*c!='\0')
  612.                 {
  613.             if(*last_last_c=='\n' &&
  614.                *last_c=='.' &&
  615.                     *c=='\n')
  616.                     fputc('.',fp);
  617.  
  618.                 fputc(*c,fp);
  619.                 last_last_c++;
  620.                 last_c++;
  621.                 c++;
  622.                 }
  623.         }
  624.   }
  625. */
  626.   if ((fwrite (text, len, 1, fp) * len) != len)
  627. d223 1
  628. a223 4
  629.       printf ("%s: Error appending mail to batch-folder %s\n", argv0, file);
  630.       fclose (fp);
  631.       if (text)
  632.             free (text);
  633. d228 15
  634. a242 3
  635.   fclose (fp);
  636.   if (text)
  637.     free (text);
  638. @
  639.